#!/bin/bash
###############################################################################
#       脚本名称: test_config.sh
#       功能描述: 统一配置接口(config.sh)单元测试
###############################################################################

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

source ../test_framework.sh
source ../../../scripts/config/config.sh

TEST_DIR=$(create_test_dir)

###############################################################################
# 测试用例
###############################################################################

test_config_init() {
    config_init 2>/dev/null
    
    # 验证YAML处理器已加载
    assert_equals "true" "${FORMAT_HANDLERS_LOADED[yaml]:-false}"
}

test_config_load_yaml() {
    local test_file="$TEST_DIR/test.yaml"
    echo "app: {name: MyApp}" > "$test_file"
    
    config_load "$test_file" "test_yaml" 2>/dev/null
    
    assert_equals "$test_file" "${CONFIG_FILES[test_yaml]}"
    assert_equals "yaml" "${CONFIG_FILE_FORMAT[test_yaml]}"
}

test_config_load_json() {
    local test_file="$TEST_DIR/test.json"
    echo '{"app":{"name":"MyApp"}}' > "$test_file"
    
    config_load "$test_file" "test_json" 2>/dev/null
    
    assert_equals "json" "${CONFIG_FILE_FORMAT[test_json]}"
}

test_config_load_env() {
    local test_file="$TEST_DIR/test.env"
    echo "APP_NAME=MyApp" > "$test_file"
    
    config_load "$test_file" "test_env" 2>/dev/null
    
    assert_equals "env" "${CONFIG_FILE_FORMAT[test_env]}"
}

test_config_load_ini() {
    local test_file="$TEST_DIR/test.ini"
    cat > "$test_file" << 'EOF'
[app]
name=MyApp
EOF
    
    config_load "$test_file" "test_ini" 2>/dev/null
    
    assert_equals "ini" "${CONFIG_FILE_FORMAT[test_ini]}"
}

test_config_get_yaml() {
    local test_file="$TEST_DIR/get_test.yaml"
    cat > "$test_file" << 'EOF'
app:
  name: MyApp
  version: 1.0.0
EOF
    
    config_load "$test_file" "get_yaml" 2>/dev/null
    
    local name=$(config_get "get_yaml" ".app.name" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_config_get_json() {
    local test_file="$TEST_DIR/get_test.json"
    echo '{"app":{"name":"MyApp"}}' > "$test_file"
    
    config_load "$test_file" "get_json" 2>/dev/null
    
    local name=$(config_get "get_json" ".app.name" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_config_get_env() {
    local test_file="$TEST_DIR/get_test.env"
    echo "APP_NAME=MyApp" > "$test_file"
    
    config_load "$test_file" "get_env" 2>/dev/null
    
    local name=$(config_get "get_env" "APP_NAME" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_config_get_ini() {
    local test_file="$TEST_DIR/get_test.ini"
    cat > "$test_file" << 'EOF'
[app]
name=MyApp
EOF
    
    config_load "$test_file" "get_ini" 2>/dev/null
    
    local name=$(config_get "get_ini" "app.name" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_config_set_yaml() {
    local test_file="$TEST_DIR/set_test.yaml"
    echo "{}" > "$test_file"
    
    config_load "$test_file" "set_yaml" 2>/dev/null
    config_set "set_yaml" ".app.name" "TestApp" "string" 2>/dev/null
    
    local name=$(config_get "set_yaml" ".app.name" 2>/dev/null)
    assert_equals "TestApp" "$name"
}

test_config_set_json() {
    local test_file="$TEST_DIR/set_test.json"
    echo '{}' > "$test_file"
    
    config_load "$test_file" "set_json" 2>/dev/null
    config_set "set_json" ".app.name" "TestApp" "string" 2>/dev/null
    
    local name=$(config_get "set_json" ".app.name" 2>/dev/null)
    assert_equals "TestApp" "$name"
}

test_config_set_env() {
    local test_file="$TEST_DIR/set_test.env"
    touch "$test_file"
    
    config_load "$test_file" "set_env" 2>/dev/null
    config_set "set_env" "APP_NAME" "TestApp" 2>/dev/null
    
    local name=$(config_get "set_env" "APP_NAME" 2>/dev/null)
    assert_equals "TestApp" "$name"
}

test_config_set_ini() {
    local test_file="$TEST_DIR/set_test.ini"
    touch "$test_file"
    
    config_load "$test_file" "set_ini" 2>/dev/null
    config_set "set_ini" "app.name" "TestApp" 2>/dev/null
    
    local name=$(config_get "set_ini" "app.name" 2>/dev/null)
    assert_equals "TestApp" "$name"
}

test_config_delete_yaml() {
    local test_file="$TEST_DIR/delete_test.yaml"
    cat > "$test_file" << 'EOF'
app:
  name: MyApp
  temp: should_delete
EOF
    
    config_load "$test_file" "delete_yaml" 2>/dev/null
    config_delete "delete_yaml" ".app.temp" 2>/dev/null
    
    local temp=$(config_get "delete_yaml" ".app.temp" 2>/dev/null)
    assert_empty "$temp"
}

test_config_reload() {
    local test_file="$TEST_DIR/reload_test.yaml"
    echo "app: {name: MyApp}" > "$test_file"
    
    config_load "$test_file" "reload_test" 2>/dev/null
    
    # 修改文件
    echo "app: {name: ModifiedApp}" > "$test_file"
    
    # 重新加载
    config_reload "reload_test" 2>/dev/null
    
    local name=$(config_get "reload_test" ".app.name" 2>/dev/null)
    assert_equals "ModifiedApp" "$name"
}

test_config_get_file() {
    local test_file="$TEST_DIR/file_test.yaml"
    echo "app: {name: MyApp}" > "$test_file"
    
    config_load "$test_file" "file_test" 2>/dev/null
    
    local file=$(config_get_file "file_test" 2>/dev/null)
    assert_contains "$file" "file_test.yaml"
}

test_config_get_format() {
    local test_file="$TEST_DIR/format_test.json"
    echo '{}' > "$test_file"
    
    config_load "$test_file" "format_test" 2>/dev/null
    
    local format=$(config_get_format "format_test" 2>/dev/null)
    assert_equals "json" "$format"
}

test_config_default_value() {
    local test_file="$TEST_DIR/default_test.yaml"
    echo "app: {name: MyApp}" > "$test_file"
    
    config_load "$test_file" "default_test" 2>/dev/null
    
    local value=$(config_get "default_test" ".app.nonexistent" "default_value" 2>/dev/null)
    assert_equals "default_value" "$value"
}

###############################################################################
# 运行测试
###############################################################################

test_suite_start "统一配置接口 (config.sh) 单元测试"

run_test "配置系统初始化" test_config_init
run_test "加载YAML配置" test_config_load_yaml
run_test "加载JSON配置" test_config_load_json
run_test "加载ENV配置" test_config_load_env
run_test "加载INI配置" test_config_load_ini
run_test "读取YAML值" test_config_get_yaml
run_test "读取JSON值" test_config_get_json
run_test "读取ENV值" test_config_get_env
run_test "读取INI值" test_config_get_ini
run_test "写入YAML值" test_config_set_yaml
run_test "写入JSON值" test_config_set_json
run_test "写入ENV值" test_config_set_env
run_test "写入INI值" test_config_set_ini
run_test "删除YAML键" test_config_delete_yaml
run_test "重新加载配置" test_config_reload
run_test "获取配置文件路径" test_config_get_file
run_test "获取配置格式" test_config_get_format
run_test "默认值支持" test_config_default_value

test_suite_end

cleanup_test_dir "$TEST_DIR"

test_report
exit $?

